package task

import ext.log
import manager.HttpRequestManager
import manager.TaskManager
import mvc.model.Status
import org.json.JSONObject

class UpdateTempValueTask : Runnable {
    override fun run() {
        try {
            exec()
        } catch (e: Exception) {
            e.printStackTrace()
            TaskManager.DelayHandler.postDelayed(this, 5 * 1000)
        }
    }

    private fun exec() {
        val result = HttpRequestManager.refreshTempValue()
        log("温度报警: $result")
        val json = JSONObject(result)
        val max = json.getInt("temTop")
        val min = json.getInt("temBottom")
        val timeOut = json.getInt("duration")
        Status.setTempValue(max, min, timeOut)
    }
}